<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>打卡记录列表</title>
    <script type="text/javascript">

      Ext.onReady(function(){
        //检修任务管理---------------------------------------------------------
        //生成有关检修任务的Grid

        var workerDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/worker/getAllWorkers'}),
          reader:new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'worker'
          }, [
            {name:'name'},                            //用作后面的displayField:
            {name:'id'}                                //用作后面的valueField:
          ])
        });
        var name = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: workerDS,
          displayField:'name',              //store里面的值，用于显示在表面上的值
          typeAhead: true,
          allowBlank: true,
          minChars:1,
          anchor:'96%',
          mode: 'remote',
          fieldLabel:'姓名',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择人员.....',
          selectOnFocus:true
        });

        var beginTimeSearch = new Ext.ux.form.DateTimeField({
          fieldLabel: '开始时间范围',
          name: 'begin_time_search',
          format: 'Y-m-d', //格式化日期
          invalidText: "您输入的日期格式不对，格式应如：2010-01-01",
          anchor: '96%'
        });

        var endTimeSearch = new Ext.ux.form.DateTimeField({
          fieldLabel: '结束时间范围',
          name: 'end_time_search',
          format: 'Y-m-d', //格式化日期
          invalidText: "您输入的日期格式不对，格式应如：2010-01-01",
          anchor: '96%'
        });

        var trackDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/track/getAllTracks'}),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'track'
          }, [
            {name:'track_name'},
            {name:'device_id'},
            {name:'id'}
          ])
        });

        var searchTrack = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: trackDS,
          displayField:'track_name',              //store里面的值，用于显示在表面上的值
          valueField:'id',                  //store里面的值，和display联动
          typeAhead: true,
          anchor: '95%',                    //在容器中的时候，自动按照容器百分比进行调整，会覆盖宽度的属性
          mode: 'remote',
          fieldLabel:'股道选择',
          forceSelection: true,
          triggerAction: 'all',
          selectOnFocus:true
        });

        var searchButton = new Ext.Button({
          text:'搜索',
          iconCls: 'searchItem',
          handler: function(){
            recordDS.baseParams.begin_time = beginTimeSearch.getValue();
            recordDS.baseParams.end_time = endTimeSearch.getValue();
            recordDS.baseParams.name = name.getValue();
            recordDS.baseParams.track_id = searchTrack.getValue();
            recordDS.load({
              params:{
                start : 0,
                limit : pageSize
              }
            })
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            searchRecordForm.form.reset();
          }
        });

        
        recordDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/record/getRecords'
          }),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'root'
          },[
            {name:'id'},
            {
              name:'name',
              mapping: 'worker',
              convert: function(value){
                if(value){
                  return value.name
                }else{
                  return '暂无'
                }
              }
            },
            {name:'train_name'},
            {name:'team_name'},
            {name:'check_time'},
            {
              name:'track_name',
              mapping: 'track',
              convert: function(value){
                if(value){
                  return value.track_name
                }else{
                  return '暂无'
                }
              }
            },
            {
              name:'type',
              mapping: 'check_type',
              convert: function(value){
                if(value == '0'){
                  return '开始工作'
                }else if(value == '1'){
                  return '结束工作'
                }
              }
            }
          ])
        });
        var recordSM = new Ext.grid.CheckboxSelectionModel();
        var recordCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          recordSM,
          {header:'姓名',sortable:true,dataIndex:'name',width:100},
          {header:'组队',dataIndex: 'team_name',width:230},
          {header:'车列',dataIndex: 'train_name',width:150},
          {header:'股道',dataIndex: 'track_name',width:90},
          {header:'打卡时间',sortable:true,dataIndex: 'check_time',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width:150},
          {header:'打卡类别',dataIndex: 'type',width:90}
        ]);
    
        var searchRecordForm = new Ext.FormPanel({
          labelWidth: 105, // label settings here cascade unless overridden
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'right',
          bodyStyle:'padding:5px 5px 0',
          width: screenWidth-200,
          items: [
            {
              xtype: 'fieldset',
              defaults: {width: 950},
              title: '条件搜索',
              border: true,
              items: [
                {
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .31,
                      layout: 'form',
                      border: false,
                      items: [beginTimeSearch]
                    },{
                      columnWidth: .31,
                      layout: 'form',
                      border: false,
                      items: [endTimeSearch]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .31,
                      layout: 'form',
                      border: false,
                      items: [name]
                    },{
                      columnWidth: .31,
                      layout: 'form',
                      border: false,
                      items: [searchTrack]
                    },{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    },{
                      columnWidth: .18,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });

        var recordGrid =  new Ext.grid.GridPanel({
          ds: recordDS,
          cm: recordCM,
          sm: recordSM,
          width: screenWidth-200,
          height: screenHeight-350,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: recordDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',
            {
              text:'删除打卡记录',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(recordGrid.getSelectionModel().hasSelection()){
                  var rs=recordGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var recordName = '';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    recordName+=data.name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下打卡记录？<br><font color="red">' + recordName + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/record/delete',
                        method:'post',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            recordDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除打卡记录。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的打卡记录');
                }
              }
            },{
              text: '下载打卡信息',
              iconCls: 'disk',
              handler: function(){
                var begin_time = beginTimeSearch.getValue();
                var end_time = endTimeSearch.getValue();
                var worker_name = name.getValue();
                var track_id = searchTrack.getValue();
                var path="/record/printRecord?begin_time="+begin_time+"&end_time="+end_time+"&worker_name="+worker_name+"&track_id="+track_id
                window.open(path,'_blank','width=1,height=1,toolbar=no,menubar=no,location=no');
              }
            }
          ]
        });

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-180,
          frame:true,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[searchRecordForm,recordGrid]
        });

        workbenchPanel.render('recordList');
      })
    </script>
  </head>
  <body>
    <div id="recordList"></div>
  </body>
</html>
